"ui";
var Pz = open("Pz.txt", "r");
var LrrsPz = Pz.readline();
var appNamePz = Pz.readline();
var LrsdPz = Pz.readline();
var YqsdPz = Pz.readline();
Pz.close();

//var theHeight = String(device.height / 4);
//var theWidth = String(device.width / 2);
ui.statusBarColor("#AA0000");
ui.layout(
    <vertical bg="#AA0000">
    <text textSize="18sp" textColor="#000000" margin="20" textStyle="bold" textColor="#ffffff">
            钱来！
        </text>
    <ScrollView>
    <vertical background="#000000" >
        
        <text id="text" bg="#FFFFFF" gravity="left" color="#000000" size="15" marginTop="15" h="225" >
        </text>
        
        <horizontal>
            <text textSize="16sp" color="#ffffff">点这里选择👉</text>
            <spinner id="hd" bg="#ffffff" entries="从下往上|从上往下" />
        </horizontal>
        <button id="autoW" text="第一次启动点这个前往设置"align="center" style="Widget.AppCompat.Button.Colored" w="300" h="80" />
        <button id="play" text="钱来！" align="center" style="Widget.AppCompat.Button.Colored" w="300" h="80" />
        <text color="#ffffff">输入拉人人数，默认为100</text>
        <input id="Lrrs" bg="#ffffff" inputType="number" text="100"/>
        <text color="#ffffff">输入软件名，默认为TIM</text>
        <input id="appName" bg="#ffffff" text="TIM"/>
        <text color="#ffffff">输入拉人速度，默认为0</text>
        <input id="Lrsd" bg="#ffffff" inputType="number" text="0"/>
        <text color="#ffffff">输入邀请速度，默认为1500</text>
        <input id="Yqsd" bg="#ffffff" inputType="number" text="1500"/>
        <horizontal align="center">
            <button id="SavePz" text="保存配置"style="Widget.AppCompat.Button.Colored"align="center" style="Widget.AppCompat.Button.Colored" w="150" h="80" />
            <button id="defaultPz" text="恢复默认配置" style="Widget.AppCompat.Button.Colored"align="center" style="Widget.AppCompat.Button.Colored" w="150" h="80" />
        </horizontal>
        <button id="saveQ" text="查看上次邀请位置" align="center" style="Widget.AppCompat.Button.Colored" w="300" h="80" />
        <button id="deleteQ" text="清空上次邀请位置" align="center" bg="#AA0000"style="Widget.AppCompat.Button.Colored" w="300" h="80" />
        </vertical>
        </ScrollView>
    </vertical>
);
ui.Lrrs.text(LrrsPz);
ui.appName.text(appNamePz);
ui.Lrsd.text(LrsdPz);
ui.Yqsd.text(YqsdPz);

ui.text.text("💛❤️使用教程：先在TIM将需要被邀请的人(注意：如果是从下往上邀请，被邀请的第一个人应置于最下方。反之，从上往下邀请，被邀请的人应该置于最上方)❤️💛\n" + "🚫切记!不要点击搜索框否则会出现bug🚫\n" + "👉音量上关闭程序👈\n" + "首次启动点下方按键。\n设置内容：开启自启动，省点策略选择无限制");
ui.play.on("click", () => {
    jsPlay();
});
ui.autoW.on("click", () => {
    if (auto.service != null) {
        toastLog("无障碍已开启");
    } else toastLog("无障碍未开启");
    app.openAppSetting("com.toxic.qianlai");
});

ui.SavePz.on("click", () => {
    files.write("Pz.txt", ui.Lrrs.text() + "\n" + ui.appName.text() + "\n" + ui.Lrsd.text() + "\n" + ui.Yqsd.text());
    toastLog("配置保存成功");
});

ui.defaultPz.on("click", () => {
    files.write("Pz.txt", "100\nTIM\n0\n1500");
    ui.Lrrs.text("100");
    ui.appName.text("TIM");
    ui.Lrsd.text("0");
    ui.Yqsd.text("1500");
});

ui.saveQ.on("click", () => {
app.viewFile("/storage/emulated/0/群and位置.txt");
});
ui.deleteQ.on("click", () => {
    engines.execScriptFile("./deleteQ.js")
    });
    

var i = 0;
var j = 0;

function jsPlay() {
    if (i) {
        if (!e.getEngine().isDestroyed())
            e.getEngine().forceStop();
    }
    if (j) {
        if (!e1.getEngine().isDestroyed())
            e1.getEngine().forceStop();
    }
    if (auto.service == null) {
        toast("请开启脚本的无障碍服务");
        var autoPlay = engines.execScriptFile("./autoPlay.js");
        return 0;
    } else {
        var hdModel = ui.hd.getSelectedItemPosition();
        //toast("钱来！");
        if (hdModel == 0) {
            i = 1;
            e = engines.execScriptFile("./larenUp.js", {
                arguments: {
                    appName: ui.appName.text(),
                    Lrsd: ui.Lrsd.text(),
                    Yqsd: ui.Yqsd.text(),
                    Lrrs: ui.Lrrs.text()
                }
            });
        } else {
            j = 1;
            e1 = engines.execScriptFile("./larenDown.js", {
                arguments: {
                    appName: ui.appName.text(),
                    Lrsd: ui.Lrsd.text(),
                    Yqsd: ui.Yqsd.text(),
                    Lrrs: ui.Lrrs.text()
                }
            });

        }
    }

}